/*
 * Copyright 2007 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.starsource.forum.client.view;

import java.util.HashSet;
import java.util.Set;

import org.starsource.forum.client.domain.MessageObject;
import org.starsource.forum.client.handler.MessageUpdateHandler;
import org.starsource.forum.client.web.ForumService;
import org.starsource.forum.client.web.ForumServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * A simple example of an 'about' dialog box.
 */
public class FileDeleteDialog extends DialogBox {
	private final ForumServiceAsync forumService = GWT.create(ForumService.class);
	private MessageUpdateHandler messageUpdateHandler;
	
	public void setMessageUpdateHandler(MessageUpdateHandler messageUpdateHandler) {
		this.messageUpdateHandler = messageUpdateHandler;
	}

  interface Binder extends UiBinder<Widget, FileDeleteDialog> { }
  private static final Binder binder = GWT.create(Binder.class);

  @UiField Button deleteButton;
  @UiField Button closeButton;
  @UiField FileListBox fileListBox;
  
	private MessageObject messageObject;

  public FileDeleteDialog(MessageObject messageObject, MessageUpdateHandler messageUpdateHandler) {
	  this.messageObject = messageObject;
	  this.messageUpdateHandler = messageUpdateHandler;

	  // Use this opportunity to set the dialog's caption.
    setText("删除文件");
    setWidget(binder.createAndBindUi(this));

    setAnimationEnabled(true);
    setGlassEnabled(true);
    
    fileListBox.setVisibleItemCount(20);
    fileListBox.setWidth("100%");
    fileListBox.setFileList(messageObject.getResources());

//    for(int index = 0; index < messageObject.getResources().size(); index ++) {
//		  fileListBox.addItem(messageObject.getResources().get(index).getUrl());
//	  }

  }

  @UiHandler("deleteButton")
  void onDeleteClicked(ClickEvent event) {
	  Set<Integer> resourceIdList = new HashSet<Integer>();
	  for(int index = 0; index < fileListBox.getItemCount(); index ++) {
		  if (fileListBox.isItemSelected(index)) {
			  resourceIdList.add(fileListBox.getFileList().get(index).getId());
		  }
	  }
	  if (resourceIdList.size() == 0) {
		  Window.alert("请选择要删除的文件。");
		  return;
	  }
	  
	  deleteButton.setEnabled(false);
	  forumService.removeResources(resourceIdList, new AsyncCallback<Void>() {

		@Override
		public void onFailure(Throwable caught) {
			deleteButton.setEnabled(true);
		}

		@Override
		public void onSuccess(Void result) {
			deleteButton.setEnabled(true);
			hide();
			messageUpdateHandler.onMessageUpdate(messageObject);
		}
		  
	  });
	  
  }

  @UiHandler("closeButton")
  void onCloseClicked(ClickEvent event) {
    hide();
//	this.messageUpdateHandler.onMessageUpdate(messageObject);
  }
}
